<?php

namespace app\model\goods;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-GoodsTypeModel
 * Class GoodsTypeModel
 * @package app\model
 */
class GoodsTypeModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'goods_type';

    /**
     * 获取分类id集合
     * @param $pid
     * @param $field
     * @param $status
     * @param $special
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getChildList($pid, $field = '', $status = 0, $special = 0)
    {
        if (empty($pid)) {
            return "";
        }
        $return_data = [];
        $map = [
            'status' => $status,
            'parent_id' => $pid,
            'special' => $special,

        ];
        $list = self::where($map)->field($field)->select();
        if (!($list->isEmpty())) {
            foreach ($list as $value) {
                $map['parent_id'] = $value['id'];
                $list_temp = self::where($map)->field($field)->select();
                $value['child'] = $list_temp;
                $return_data[] = $value;
            }
        }
        return $return_data;
    }


    /**
     * 获取分类id集合
     * @param $pid
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getChildId($pid)
    {
        if (empty($pid)) {
            return "";
        }
        $return_data[] = $pid;
        $list = self::where(['parent_id' => $pid])->select();
        if (!($list->isEmpty())) {
            foreach ($list as $value) {
                $return_data[] = $value['id'];
                $list_temp = self::where(['parent_id' => $value['id']])->select();
                foreach ($list_temp as $val) {
                    $return_data[] = $val['id'];
                }
            }
        } else {
            $return_data = $pid;
        }
        return $return_data;
    }


    /**
     * 获取顶级分类
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getParentId($id)
    {
        $pas = self::where(['id' => $id])->find();
        if (!empty($pas)) {
            if (intval($pas['parent_id']) == 0) {
                return $pas['id'];
            }
            $parent_id = self::getParentId($pas['parent_id']);
            if (!empty($parent_id)) {
                return $parent_id;
            }
        }
        return 0;
    }

    /**
     * 获取分类名称
     * @param string $id
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getTypeName($id = '')
    {
        $type = self::where('id', $id)->find();
        if (empty($type)) {
            return "";
        }
        return $type['name'];
    }
}
